# To change this template, choose Tools | Templates
# and open the template in the editor.
require 'RMagick'
include Magick
module CommonFunction
  def crop_image
    begin
      img = Magick::Image::read(tmpfile).first
      unless width == 0 or height == 0
        img.crop!(x1,y1,width,height)
      end
      img.thumbnail!(120, 150)
      img.write(outfile){ self.quality = 95; self.density = 92; }
    rescue Exception => err
      @upload_image_error = 'Could not process your image file.  Pleasetry again.' + err
          
    end
  end
  def self.upload_file(file,path="/public/images/")
    unless file.original_filename.empty?
      #@filename=getFileName(file.original_filename)
      #@filename=file.original_filename.gsub(/[^\w.]/,'')
      #@filename=Time.now.to_s(:db).gsub(/[-:]|\s/,"")+File.extname(file.original_filename)
      @filename=Time.now.strftime("%Y%m%d%H%M%S") + rand(10000).to_s + File.extname(file.original_filename)
      File.open("#{RAILS_ROOT}#{path}#{@filename}", "wb") { |f| f.write(file.read) }
      return @filename
    end
  end
  def self.get_thumnb(filename,size=130,overwrite=nil)
    if filename
      geom = "#{size}x#{size}"
      # Read the image and resize it. The `change_geometry' method
      # computes the new image geometry and yields to a block. The
      # return value of the block is the return value of the method.
      filename=RAILS_ROOT+filename

      img = Image.read(filename)[0]
      img.change_geometry!(geom) { |cols, rows| img.thumbnail! cols, rows }
      overwrite ? img.write(filename) : img.write(filename.gsub!(".","_s."))
      
      # We need a background to display the thumbnail.
      # Create a square, neutral gray background with raised edges.
      # Make this background slightly larger than the image to allow
      # for the raised border. A 3-pixel raised edge means that the
      # background needs to be 6 pixels larger in each dimension.

      # bg = Image.new(size+6, size+6) { self.background_color = "gray75" }
      #bg = bg.raise(3,3)

      # Just for the purposes of this example, display the thumbnail background on
      # a larger white background.

      #white_bg = Image.new(size+50, size+50) {self.background_color = "white"}
      #white_bg = white_bg.composite(bg, CenterGravity, OverCompositeOp)

      # Finally, center the thumbnail on the gray background.
      #thumbnail = white_bg.composite(img, CenterGravity, OverCompositeOp)

      # thumbnail.write('e:/rubyapplication/meibaiba_psd/thumbnail.gif')
    end
  end
  #显示列表页面的内容
  def self.dis_page_entries_info(collection, options = {})
      entry_name = options[:entry_name] || (collection.empty?? '空' : collection.first.class.name.underscore.sub('_', ' '))

      if collection.total_pages < 2
        case collection.size
        when 0; "没有找到 #{entry_name}"
        when 1; "显示 <b>1</b> #{entry_name}"
        else;   "总共 <b> #{collection.size}</b> #{entry_name}"
        end
      else
        %{显示 <b>%d</b> #{entry_name}中的第 <b> %d&nbsp;-&nbsp;%d</b> #{entry_name} } % [
          collection.total_entries,
          collection.offset + 1,
          collection.offset + collection.length          
        ]
      end
    end

end
